package top.deepdesigner.controller.promotion;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsArticle;
import top.deepdesigner.service.DtsArticleService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/24 星期六 15:34:37 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/article")
public class ArticleController {

  @Autowired
  private DtsArticleService articleService;

  @RequiresPermissionsDesc(menu = {"推广管理", "公告列表"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:article:list"})
  @GetMapping("/list")
  public Object list(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "20") Integer limit,
                     @RequestParam(defaultValue = "add_time") String sort,
                     @RequestParam(defaultValue = "desc") String order,
                     @RequestParam(defaultValue = "") String title) {
    CommonListVO<DtsArticle> articleListVO = new CommonListVO<>();
    //获取分页数量
    Long count = articleService.getArticleCount();
    List<DtsArticle> dtsArticles = articleService.getArticleList(page, limit, sort, order, title);
    articleListVO.setTotal(count);
    articleListVO.setItems(dtsArticles);
    return ResponseUtil.ok(articleListVO);
  }

  @RequiresPermissionsDesc(menu = {"推广管理", "发布公告"}, button = "添加")
  @RequiresPermissions(value = {"admin:article:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsArticle article) {
    DtsArticle dtsArticle = articleService.createArticle(article);
    if (dtsArticle == null) return ResponseUtil.fail(700, "关键字已存在");
    return ResponseUtil.ok(dtsArticle);
  }

  @RequiresPermissionsDesc(menu = {"推广管理", "公告列表"}, button = "修改详情")
  @RequiresPermissions(value = {"admin:article:detail"})
  @GetMapping("/detail")
  public Object detail(@RequestParam Integer id) {
    DtsArticle dtsArticle = articleService.getArticleById(id);
    return ResponseUtil.ok(dtsArticle);
  }
  @RequiresPermissionsDesc(menu = {"推广管理", "公告列表"}, button = "修改")
  @RequiresPermissions(value = {"admin:article:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsArticle article) {
    DtsArticle dtsArticle = articleService.updateArticle(article);
    return ResponseUtil.ok(dtsArticle);
  }

  @RequiresPermissionsDesc(menu = {"推广管理", "公告列表"}, button = "删除")
  @RequiresPermissions(value = {"admin:article:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsArticle article) {
    articleService.deleteArticle(article);
    return ResponseUtil.ok();
  }
}
